"""
OpenCV 读取摄像头视频视频流，并在画面上绘制文字和图形
"""
 
# 导入OpenCV
import cv2
import time
 
import drawUtils

cap = cv2.VideoCapture(0)
width = 1920  # 宽度  
height = 1080  # 高度
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)  
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)  
# 设置帧率，例如设置为30fps  
# fps = 50.0
# cap.set(cv2.CAP_PROP_FPS, fps)  

start_time = time.time()
 
while True:
     
    # 读取每一帧
    ret,frame = cap.read()

    # 计算FPS
    now = time.time()
    fps_text  = int(1 / ( now - start_time))
    start_time = now
 
    # 添加中文（首先导入模块）
    frame = drawUtils.cv2AddChineseText(frame, 'rate:'+str(fps_text), (20,50), textColor=(0, 255, 0), textSize=30)
 
    # 显示画面
    cv2.imshow('XiaoJieXueZhang',frame)
 
    # 退出条件
    if cv2.waitKey(10) & 0xFF == 27:
        break
 
 
cap.release()
cv2.destroyAllWindows()